package com.lgt_enterprise_android.view.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.adapter.YFQBAdapter;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.view.pages.b.ArrangeJobActivity;
import com.lgt_enterprise_android.view.pages.me.EnterpriseActivity;
import com.my_view_library.pulltorefresh.library.PullToRefreshBase;
import com.my_view_library.pulltorefresh.library.PullToRefreshListView;
import com.util.AppUtil;


public class FirstFragment extends Fragment {


    PullToRefreshListView mPullToRefreshListView;

    YFQBAdapter adapter;

    JSONArray jsonArray = new JSONArray();

    private int pageIdx = 1;

    private View viewNoCompany;
    private View viewNoQB;

    public FirstFragment() {
        // Required empty public constructor
    }

    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPullToRefreshListView = (PullToRefreshListView) getActivity().findViewById(R.id.first_listview);
        viewNoCompany=getActivity().findViewById(R.id.frame_no_company);
        viewNoQB=getActivity().findViewById(R.id.frame_no_qb);
        mPullToRefreshListView.getRefreshableView().setDivider(getActivity().getDrawable(R.color.transparent));
        mPullToRefreshListView.getRefreshableView().setDividerHeight(AppUtil.getdensityIntDpi(getActivity()) * 15);
        adapter=new YFQBAdapter(getActivity(),jsonArray);

        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIdx=1;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIdx++;
                getData();
            }
        });
        mPullToRefreshListView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!isHidden()){
            getBranch();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            getBranch();
        }
    }

    private void getData(){
        HttpManager.getQiangBanList(getActivity(), pageIdx, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                mPullToRefreshListView.onRefreshComplete();
                if(pageIdx==1){
                    JSONObject jsonObject=(JSONObject)resonseObj;

                    JSONArray arr=jsonObject.getJSONArray("list");

                    if(arr.size()==0){
                        viewNoQB.setVisibility(View.VISIBLE);
                        TextView txtGoto = (TextView) getActivity().findViewById(R.id.txt_goto_qb);
                        SpannableString spannable = new SpannableString(txtGoto.getText().toString());
                        spannable.setSpan(new ForegroundColorSpan(0xff27A9F8),5, 9,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        spannable.setSpan(new UnderlineSpan(),5, 5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        txtGoto.setText(spannable);
                        txtGoto.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent=new Intent(getActivity(), ArrangeJobActivity.class);
                                intent.putExtra("type",2);
                                startActivity(intent);
                            }
                        });
                        return;
                    }
                    viewNoQB.setVisibility(View.GONE);
                    if(arr.size()<15){
                        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }else{
                        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
                    }
                    adapter.setData(arr);
                    adapter.notifyDataSetChanged();
                }else{
                    adapter.addData(jsonArray);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String mes) {

            }
        });
    }


    private void getBranch(){
        HttpManager.getBranch(getActivity(), new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                if(getActivity().isFinishing())return;
                JSONArray jsonArray = (JSONArray) resonseObj;
                if (jsonArray.size() == 0) {
                    viewNoCompany.setVisibility(View.VISIBLE);
                    TextView txtGoto = (TextView) getActivity().findViewById(R.id.txt_goto);
                    SpannableString spannable = new SpannableString(txtGoto.getText().toString());
                    spannable.setSpan(new ForegroundColorSpan(0xff27A9F8),3, 7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannable.setSpan(new UnderlineSpan(),3, 7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    txtGoto.setText(spannable);
                    txtGoto.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent=new Intent(getActivity(), EnterpriseActivity.class);
                            startActivity(intent);
                        }
                    });

                } else {
                    getActivity().findViewById(R.id.frame_no_company).setVisibility(View.GONE);
                    HttpManager.selectWhichBranch(getActivity(), jsonArray.getJSONObject(jsonArray.size()-1).getString("id"), new BasicHttp.IMyCallBack() {
                        @Override
                        public void onResponse(Object seleltBranchObj) {
                            getData();
                        }

                        @Override
                        public void onFailure(String mes) {
                        }
                    });
                }
            }

            @Override
            public void onFailure(String mes) {

            }
        });
    }
}
